﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Linq;
using System.Web;

namespace PragmaticMVC.Models
{
    public static class UsersRolesContext
    {
        public class UserDTO
        {
            private string userName;
            private string password;

            public string UserName
            {
                get { return userName; }
                set { userName = value; }
            }

            public string Password
            {
                get { return password; }
                set { password = value; }
            }
        }

        public static int GetUsers()
        {
            int res = 0;
            using (var urcEDM = new PragmaticMVCEntities())
            {
                var userNames = from users in urcEDM.UserSet
                                select users.UserName;

                foreach (string userName in userNames)
                    Console.WriteLine(userName);
                res = userNames.Count();
            }
            return res;
        }

        public static bool GetUser(string userName, string password)
        {
            // var userDTO = new UserDTO();

            bool userExist = false;

            using (var urcEdm = new PragmaticMVCEntities())
            {
                var user = from users in urcEdm.UserSet
                           where users.UserName == userName && users.Password == password
                           select users.UserName;

                string tempName = user.FirstOrDefault() ?? "null";
                if (tempName != "null")
                    userExist = true;

            }

            return userExist;
        }

        public static string[] GetRolesForUser(string userName)
        {
            List<string> userRoles = new List<string>();

            using (var urcEdm = new PragmaticMVCEntities())
            {

                var roles = from u in urcEdm.UserSet
                            from r in u.Roles
                            where u.UserName == userName
                            select r.RoleName;

                foreach(string role in roles)
                {
                    userRoles.Add(role);
                    Console.WriteLine(role);
                }
            }
            return userRoles.ToArray();
        }

        public static string[] GetRolesByContext(string contextName)
        {
            List<string> contextRoles = new List<string>();

            using (var urcEdm = new PragmaticMVCEntities())
            {
                var roles = from f in urcEdm.FeatureSet
                            from r in f.Roles
                            where f.ContextName == contextName
                            select r.RoleName;

                foreach(string role in roles)
                {
                    contextRoles.Add(role);
                }
           
            }

            return contextRoles.ToArray();
        }

        public static bool IsUserInRole(string userName, string roleName)
        {
            bool res = false;
            using (var urcEdm = new PragmaticMVCEntities())
            {
                var exists = from u in urcEdm.UserSet
                            from r in u.Roles
                            where u.UserName == userName
                                  && r.RoleName == roleName
                            select u.UserName;

                if (exists.FirstOrDefault() != null)
                    res = true;
                
            }
            return res;
        }

        public static bool IsRoleInContext(string roleName, string contextName)
        {
              bool res = false;
            using (var urcEdm = new PragmaticMVCEntities())
            {
                var exists = from f in urcEdm.FeatureSet
                            from r in f.Roles
                            where f.ContextName == contextName
                                  && r.RoleName == roleName
                            select f.ContextName;

                if (exists.FirstOrDefault() != null)
                    res = true;
               
            }
            return res;
        }
    }
}